import React, { useEffect, useState } from 'react'
import baseUrl from '../../axios/config'
import { Button, Input, message } from 'antd'
import {useNavigate}from 'react-router-dom'
import './Login.scss'
export default function Login() {
  const [value, setValue] = useState('')
  const navigate = useNavigate()

//如果toen  有数据就跳转到/home页面

  useEffect(()=>{
     const token = localStorage.getItem("token")
     if(token){
        navigate('/home')
     }
    })


  const submit = () => {
    if(value===""){
      message.info("用户名不能为空")
      return
    }
    if(value==="tanpi"){
      navigate('/server')

    }

    baseUrl.post('/xiaolu/lg',{
      name:value
    }).then((response) => {

console.log(response.data)
const loginid = response.data.id
switch (loginid) {

  case "用户名不对":
    message.info("用户名不对"),setValue(''), window.location.reload()
    break;
  case "登录成功":

    localStorage.setItem('token', response.data.token),navigate('/home') , localStorage.setItem('userinfo', value)

    break;

}


    }).catch((error) => {
      message.info("提交服务器失败")
    })


  }
  console.log(value)


  return (
    <div>
      <div className='contain'>
        <h1  className='text'>      请登录你的账号</h1>
      </div>


      <div    className='Input'>

      <Input    value={value}    onChange={(e)=>{setValue(e.target.value)}} style={{width:300}}/>


      </div>

<div className='button'>
  <Button type="primary" onClick={submit}>登录</Button>
</div>
    </div>
  )
}
